/*
 * WorldObject.java
 *
 * Created on 18. Mai 2007, 19:47
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.tor.obj;

import com.golden.gamedev.object.SpriteGroup;
import de.tor.loaders.Level;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Charon
 */
public abstract class AbstractWorldObject extends TorridityGameObject{
    
    private Hashtable<Point, Enemy> mExtensions = null;
    private List<Point> mExtensionPoints = null;
    private SpriteGroup mExtensionsGroup = null;
    
    /** Creates a new instance of WorldObject */
    public AbstractWorldObject(Level pLevel, BufferedImage pView) {
        super(pLevel, pView);
        mExtensions = new Hashtable();
        mExtensionPoints = new LinkedList();
        mExtensionsGroup = new SpriteGroup("Extensions");
    }
    
    public AbstractWorldObject(Level pLevel, BufferedImage[] pView) {
        super(pLevel, pView);
        mExtensions = new Hashtable();
        mExtensionPoints = new LinkedList();
        mExtensionsGroup = new SpriteGroup("Extensions");
    }
    
    public void addChild(Point mExtensionPoint, Enemy pChild){
        if(mExtensionPoints.contains(mExtensionPoint)){
            pChild.setHorizontalSpeed(getHorizontalSpeed());
            pChild.setVerticalSpeed(getVerticalSpeed());
            mExtensions.put(mExtensionPoint, pChild);
            mExtensionsGroup.add(pChild);
        }
    }
    
    public SpriteGroup getExtensionsGroup(){
        return mExtensionsGroup;
    }
    
    public void addExtensionPoint(Point exPoint){
        mExtensionPoints.add(exPoint);
    }
    
    public Enemy getExtension(Point p){
        return mExtensions.get(p);
    }
    
    public void setVerticalSpeed(double d){
        super.setVerticalSpeed(d);
        for(Point ex : mExtensionPoints){
            Enemy exEnemy = mExtensions.get(ex);
            if(exEnemy != null) exEnemy.setVerticalSpeed(d);
        }
    }
    
    public void setHorizontalSpeed(double d){
        super.setHorizontalSpeed(d);
        for(Point ex : mExtensionPoints){
            mExtensions.get(ex).setHorizontalSpeed(d);
        }
    }
    
    public Point getExtensionPoint(int idx){
        if(idx > mExtensionPoints.size()-1){
            return mExtensionPoints.get(0);
        }
        return mExtensionPoints.get(idx);
    }
    
    public abstract void updateWorldObject();
    
    public void updateTorridityGameObject(long elapsedTime){
        updateWorldObject();
        for(Point exPoint: mExtensionPoints){
            Enemy child = mExtensions.get(exPoint);
            if(child != null){
                child.setLocation(getX() + exPoint.x - child.getWidth()/2, getY() + exPoint.y - child.getHeight()/2);
                child.setLayer(getLayer()+1);
                child.updateTorridityGameObject(elapsedTime);
            }
        }
    }
}
